package top.yuki.event;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import top.yuki.Chat.ChatCharacteristic;
import top.yuki.Inventory.Package;
import top.yuki.LevelWorld;
import top.yuki.board.PlayerBoard;
import top.yuki.data.DataManager;
import top.yuki.data.WorldManage;

import java.io.File;


public class onPlayerWorldChange implements Listener {
    public DataManager dataManage;
    public WorldManage worldManage;
    private ConfigurationSection config;
    public LevelWorld lw = LevelWorld.instance;
    public onPlayerWorldChange (LevelWorld lw){
        this.dataManage = lw.dataManage;
        this.worldManage =lw.worldManage;
        this.config = lw.getConfig();
    }
    @EventHandler
    public void onChangeWorld(PlayerChangedWorldEvent event){
        try {
            Player player = event.getPlayer();
            String id = String.valueOf(player.getUniqueId());
            World fromWorld = event.getFrom();
            World arrivalWorld = player.getWorld();
            int fromWorldLevel = worldManage.getWorldLevel(fromWorld.getName());
            int arrivalWorldLevel = worldManage.getWorldLevel(arrivalWorld.getName());
            PlayerBoard.refresh(player);
            if (fromWorldLevel == arrivalWorldLevel) {
                return;
            }
            if (arrivalWorldLevel == 0){
                Package.savePackage(fromWorldLevel,player);
                Package.joint(fromWorldLevel,0,player);
                player.sendMessage(ChatCharacteristic.SYSTEM_CAT.system+"抵达0级世界，已切换背包");
            }else if(fromWorldLevel>arrivalWorldLevel){
                Package.savePackage(fromWorldLevel,player);
                Package.joint(fromWorldLevel,arrivalWorldLevel,player);
                player.sendMessage(ChatCharacteristic.SYSTEM_CAT.system+"抵达"+arrivalWorldLevel+"级世界，已切换背包");
            }else{
                int playerWorldLevel = dataManage.getWorldLevel(player.getUniqueId());
                if (arrivalWorldLevel>playerWorldLevel) {
                    Package.savePackage(fromWorldLevel,player);
                    Package.joint(fromWorldLevel,arrivalWorldLevel,player);
                    player.performCommand("spawn");
                    player.sendMessage("无法抵达高级世界");
                    return;
                }else{
                    Package.savePackage(fromWorldLevel,player);
                    Package.joint(fromWorldLevel,arrivalWorldLevel,player);
                    player.sendMessage(ChatCharacteristic.SYSTEM_CAT.system+"抵达"+arrivalWorldLevel+"级世界，已切换背包");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()) {
                onlinePlayer.kickPlayer("服务器发生致命错误，已关闭,请联系腐竹");
            }
            Bukkit.getServer().shutdown();
        }




    }

}
